package com.orientation.commom.config;

import cn.hutool.core.util.StrUtil;
import com.orientation.commom.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @PROJECT_NAME: orientation
 * @PACKAGE_NAME: com.orientation.commom.config
 * @NAME: LoginInterceptor
 * @author:xuandian
 * @DATE: 2022/1/18 17:56
 * @description: 登录拦截器
 */
@Slf4j
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)  {
        String token = request.getHeader("token");
        log.error("获取token：token：{}，", token);
        String url=request.getRequestURI();
        log.error("获取token：url：{}，", url);
        if (StrUtil.isBlank(token)) {
            throw new CustomException("401", "未获取到token, 请重新登录");
        }
        // String method=request.getMethod();
        // if (!"OPTIONS".equals(method)){
        //     throw new CustomException("401", "错误, 请重新登录");
        // }
        return true;
    }
    //
    // @Override
    // public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    //
    // }
    // public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    //
    // }
}
